package cn.school.job.attendance;

import cn.school.attendance.dormitory.dao.DormitoryAttendanceMapper;
import cn.school.attendance.dormitory.entity.DormitoryAttendanceDo;
import cn.school.attendance.dormitory.pojo.AttendanceDto;
import cn.school.workflow.dao.StudentLeaveApplyDao;
import cn.school.workflow.dao.StudentLeaveSelfApplyDao;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author: lx
 * @date: 2024/3/23 11:51
 * @description:
 */
@Slf4j
public class StudentAttendanceUpdateJob extends QuartzJobBean {

    @Autowired
    DormitoryAttendanceMapper attendanceMapper;

    @Autowired
    StudentLeaveSelfApplyDao studentLeaveSelfApplyDao;

    @Autowired
    StudentLeaveApplyDao studentLeaveApplyDao;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("StudentAttendanceUpdateJob ======>  每日考勤数据更新");

        Date today = new Date();
        SimpleDateFormat ymdsdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat ymdhmssdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String ymd = ymdsdf.format(today);
        String ymdhms = ymdhmssdf.format(today);
//        String todayStr = format.format(today);
        //查询当天所有请假(自提和代提，代提表学生id为学生表主键，自提表学生id为学生表学号)
        List<String> leaveStuList = studentLeaveApplyDao.getLeaveStuList(ymdhms);
        List<String> selfLeaveStuList = studentLeaveSelfApplyDao.getSelfLeaveStuList(ymdhms);
        List<AttendanceDto> allStuAttendanceDate = attendanceMapper.getAllStuAttendanceDate(ymd);

        int leave = 0;
        int absence = 0;
        for(AttendanceDto item : allStuAttendanceDate){
            //判断当前考勤数据是否存在与自提和代提请假数据中
            if(leaveStuList.contains(String.valueOf(item.getStudentId()))
                    || selfLeaveStuList.contains(item.getStudentNo())){
                if("2".equals(item.getAttendanceStatus())){
                    continue;
                }
                DormitoryAttendanceDo attendance = new DormitoryAttendanceDo();
                attendance.setId(item.getAttendanceId());
                attendance.setStatus("2");
                attendance.setModifyTime(ymdhmssdf.format(today));
                attendance.setModifyBy("sysJob");
                int update = attendanceMapper.updateById(attendance);
                if(update > 0){
                    leave++;
                }
                //当前考勤数据不存在于请假数据中
            }else {
                //且为请假状态，则将请假状态修改为未归寝
                if("2".equals(item.getAttendanceStatus())){
                    DormitoryAttendanceDo attendance = new DormitoryAttendanceDo();
                    attendance.setId(item.getAttendanceId());
                    attendance.setStatus("0");
                    attendance.setModifyTime(ymdhmssdf.format(today));
                    attendance.setModifyBy("sysJob");
                    int update = attendanceMapper.updateById(attendance);
                    if(update > 0){
                        absence++;
                    }
                }
            }
        }
        log.info("StudentAttendanceUpdateJob ===>  【" + ymdhmssdf.format(today) + "】更新考勤为请假的学生共有：" + leave + "人");
        log.info("StudentAttendanceUpdateJob ===>  【" + ymdhmssdf.format(today) + "】更新考勤为缺勤的学生共有：" + absence + "人");
    }
}
